﻿// <copyright file="MainPage.xaml.cs" company="Waveform Lab.NET">
// Signal processing by PC oscilloscope
// http://waveformlab.codeplex.com/
//
// Copyright (c) 2011 Waveform Lab.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using MemoryModule;
using AudioModule;
using F2fModule;
using FFTModule;
using Chart;

namespace WaveformLab_SL
{
    public partial class MainPage : UserControl
    {
        private enum SignalProcessing { None, FFT, F2F };
        private SignalProcessing Proc = SignalProcessing.None;
        //private ChartControl Graph;
        //private AudioModueView Dev;
        private FFTModuleView fftobj;
        //private F2fModuleView f2fobj;
        private util dir;

        public MainPage()
        {
            InitializeComponent();
/*
            AudioModueView Dev = new AudioModueView();
            LayoutRoot.Children.Add(Dev);
            ChartControl Graph = new ChartControl();
            LayoutRoot.Children.Add(Graph);
*/ 
            Proc = new SignalProcessing();
            Proc = SignalProcessing.F2F;

            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
            dir = new util();
        }

        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            if (Dev == null) return;

            if (Dev.Captured)
            {
                if ((Dev.EnableLeft == false) && (Dev.EnableRight == false)) return;
                string name = dir.BaseDir + "sampling" + Dev.BufferNo.ToString() + ".csv";
                dir.Name = name;
                string name_l = dir.BaseDir + "sample_l" + Dev.BufferNo.ToString() + ".csv";
                string name_r = dir.BaseDir + "sample_r" + Dev.BufferNo.ToString() + ".csv";
/*
                try { var r = new StreamReader(name); }
                catch { return; }
                try { var r = new StreamReader(name_l); }
                catch { return; }
                try { var r = new StreamReader(name_r); }
                catch { return; }
*/
                switch (Proc)
                {
                    case SignalProcessing.FFT:
                        FFTProc(name_l, name_r);
                        Graph.LoadCsv(fftobj.FileName, fftobj.NameX, fftobj.NameY);
                        Graph.ShowAxis();
                        break;
                    case SignalProcessing.F2F:
                        Graph.LoadCsv(name, Dev.NameX, Dev.NameY);
                        Graph.ShowAxis();
                        F2FProc(Dev.seq_start_l, Dev.seq_start_r, name_l, name_r);
                        break;
                    default:
                        Graph.LoadCsv(name, Dev.NameX, Dev.NameY);
                        Graph.ShowAxis();
                        break;
                }

                Dev.Captured = false;
            }
        }

        private void FFTProc(string l, string r)
        {
            fftobj.NyquistRate = Dev.SampleRate / 2;
            fftobj.ActualSamplingNumber = Dev.SampleNumber;
            string msg = fftobj.Processing(l, r, Dev.EnableLeft, Dev.EnableRight);
            textBoxConsole.Text = msg;
        }

        private void F2FProc(int sl, int sr, string l, string r)
        {
            string msg = f2fobj.Processing(sl, sr, l, r, Dev.EnableLeft, Dev.EnableRight);
            textBoxConsole.Text = msg;
        }
    }
}
